package controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pojo.Doctor;
import common.Result;
import service.DoctorService;
import org.springframework.stereotype.Controller;
import java.util.List;

@RestController
@CrossOrigin(origins = "*", allowedHeaders = "*", methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT, RequestMethod.DELETE, RequestMethod.OPTIONS})
@RequestMapping("/doctor")
@Api(tags = "医生管理接口", description = "提供医生相关的增删改查功能")
public class DoctorController {

    @Autowired
    private DoctorService doctorService;

    @GetMapping("/findAll")
    @ResponseBody
    @ApiOperation(value = "查询所有医生", notes = "获取系统中所有医生的信息")
    public List<Doctor> findAll() {
        return doctorService.findAll();
    }

    @GetMapping("/findById")
    @ResponseBody
    @ApiOperation(value = "根据ID查询医生", notes = "通过医生ID获取具体医生信息")
    @ApiImplicitParam(name = "docId", value = "医生ID", required = true, dataType = "String", paramType = "query")
    public Doctor findById(@RequestParam String docId) {
        return doctorService.findById(docId);
    }

    @GetMapping("/findByUserName")
    @ResponseBody
    @ApiOperation(value = "根据user_id查询医生", notes = "通过用户ID获取关联的医生信息")
    @ApiImplicitParam(name = "username", value = "用户ID", required = true, dataType = "String", paramType = "query")
    public Result<Doctor> findByUserName(@RequestParam String user) {
        try {
            Doctor doctor = doctorService.findByUserName(user);
            if (doctor == null) {
                return Result.error("未查询到该用户ID对应的医生信息");
            }
            return Result.success(doctor);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("查询医生信息失败，请联系管理员");
        }
    }

    @GetMapping("/findByUserId/{userId}")
    @ResponseBody
    @ApiOperation(value = "根据user_id查询医生", notes = "通过用户ID获取关联的医生信息")
    @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "String", paramType = "path")
    public Result<Doctor> findByUserId(@PathVariable String userId) {
        try {
            Doctor doctor = doctorService.findByUserId(userId);
            if (doctor == null) {
                return Result.error("未查询到该用户ID对应的医生信息");
            }
            return Result.success(doctor);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("查询医生信息失败，请联系管理员");
        }
    }
    @PostMapping("/add")
    @ResponseBody
    @ApiOperation(value = "添加医生", notes = "新增医生信息到系统")
    public int add(@RequestBody Doctor doctor) {
        return doctorService.add(doctor);
    }

    @PostMapping("/update")
    @ResponseBody
    @ApiOperation(value = "更新医生信息", notes = "根据医生ID更新其信息")
    public int update(@RequestBody Doctor doctor) {
        return doctorService.update(doctor);
    }

    // 新增：根据user_id更新医生信息的接口
    @PostMapping("/update/{userId}")
    @ResponseBody
    @ApiOperation(value = "根据user_id更新医生信息", notes = "通过用户ID更新对应的医生信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "doctor", value = "医生对象", required = true, dataType = "Doctor", paramType = "body")
    })
    public int updateByUserId(@PathVariable String userId, @RequestBody Doctor doctor) {
        // 设置user_id，确保更新时使用路径中的user_id
        doctor.setUserId(userId);
        return doctorService.updateByUserId(doctor);
    }

    @PostMapping("/delete")
    @ResponseBody
    @ApiOperation(value = "删除医生", notes = "根据医生ID删除该医生信息")
    @ApiImplicitParam(name = "docId", value = "医生ID", required = true, dataType = "String", paramType = "query")
    public int delete(@RequestParam String docId) {
        return doctorService.deleteDoctor(docId);
    }
}